#-*-coding:utf-8-*-
'''
Created on 2011-1-16

@author: zhaoyh
@version: 0.1
'''
import web
from admin.config import view
from db.auth import Group
from core.meta import PageList,DBSession,makewhere
import admin.models.group as m_group
from utils.utility import AppStatus

compo_name='用户组'
class list():
    '''
          请求页面时使用get，提交数据时使用post
    '''
    
    def GET(self,name):
        page=web.input(numPerPage=20,pageNum=1,keywords='',orderField='group_code',col='')
        #titles={'group_code':'用户组代码','group_name':'用户组名称','note':'备注'}    
        titles=web.Storage({'group_code':'用户组代码','group_name':'用户组名称','note':'备注'})
        where=makewhere(page.keywords,page.col,titles)        
        list=PageList(Group,page,where)
        return view.group_list(list=list.list(),page=list.Pageinfo(),title=titles)
    def POST(self,name):
        return self.GET(name)
class add():
    def GET(self):
        return view.group_add()
    def POST(self):
        u=web.input(group_code=None,add_next=None)
        if u.group_code==None:return self.GET()
        m=m_group.GroupModel()
        status=AppStatus()
        if m.check_code_only(u.group_code):
            status.statusCode='error'
            status.message='代码重复，请修改后保存'
        else:
            if m.add(group_code=u.group_code, group_name=u.group_name,note=u.note):
                status.statusCode='ok'
                status.message='添加成功'
                status.navtabId="group_list"
                status.navtaburl="/admin/group/"
            else:
                status.statusCode='error'
                status.message='添加失败，请与管理员联系'  
        print  status.status()             
        return status.status()
class edit():
    def GET(self,group_id):
        edit_Group=DBSession.query(Group).filter(Group.group_id==group_id).first()
        if edit_Group==None:
            return  '{"statusCode":"300", "message":"数据已删除请刷新后重试"}'
        else:
            return view.group_edit(group=edit_Group)
    def POST(self,id):
        u=web.input()
        e=m_group.GroupModel()
        if e.edit(u.group_id, group_code=u.group_code, group_name=u.group_name,note=u.note):
            return '{"statusCode":"200", "message":"编辑成功"}'
        else:
            return '{"statusCode":"300", "message":"错误，请与管理员联系"}'
        
class delete():
    """
    TODO:批量删除怎么处理，应该校验下是否使用如果使用就不能删除
    """
    def POST(self,group_id):
        u=web.input(ids=[])
        print u
        error_list=[]
        ok_list=[]
        for id in u.ids:
            e=m_group.GroupModel()
            if e.delete(id):
                ok_list.append(id)
            else:
                error_list.append(id)
        if error_list:
            return '{"statusCode":"300", "message":"%s删除错误，请与管理员联系"}' %error_list
        else:
            return '{"statusCode":"200", "message":"删除成功"}'

